﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
namespace IMS_WEB.controls
{
    [ToolboxData("<{0}:TextBox runat=\"server\" />")]
    public class TextBox : System.Web.UI.WebControls.TextBox
    {
        private Hashtable _values;
        public DropDownList _DropDownList;

        public delegate void ItemIndexChanged(object obj, EventArgs e);

        public event ItemIndexChanged myIndexChanged;
        public TextBox()
        {
            _DropDownList = new DropDownList();
            _values = new Hashtable();
            _DropDownList.AutoPostBack = true;
            _DropDownList.SelectedIndexChanged += new EventHandler(_DropDownList_SelectedIndexChanged);
        }

        void _DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (myIndexChanged != null)
            {
                myIndexChanged(sender, e);
            }
        }

        public Hashtable Values
        {
            get
            {
                return _values;
            }
            set
            {
                _values = value;
            }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter Output)
        {
            int iWidth = Convert.ToInt32(base.Width.Value);
            if (iWidth == 0)
            {
                iWidth = 102;
            }
            int sWidth = iWidth + 16;
            int spanWidth = sWidth - 18;
            Output.Write("<div style=\"POSITION:relative\">");
            Output.Write("<span style=\"MARGIN-LEFT:" + spanWidth + "px;OVERFLOW:hidden;WIDTH:18px\">");
            _DropDownList.Width = Unit.Parse(sWidth + "px");
            _DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth + "px");
            _DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value");
            if (_values.Count > 0)
            {
                foreach (string key in _values.Keys)
                {
                    ListItem item = new ListItem();
                    item.Value = key;
                    item.Text = _values[key].ToString();
                    _DropDownList.Items.Add(item);
                }
            }

            if (_DropDownList.Items.Count == 1)
            {
                ListItem item = new ListItem();
                item.Value = "";
                item.Text = " ";
                _DropDownList.Items.Add(item);
                _DropDownList.SelectedIndex = 1;
            }
            _DropDownList.RenderControl(Output);
            Output.Write("</span>");
            base.Style.Clear();
            base.Width = Unit.Parse(iWidth + "px");
            base.Style.Add("left", "0px");
            base.Style.Add("POSITION", "absolute");
            base.Render(Output);
            Output.Write("</div>");
        } 
    }
}